package wens.pig.dhm.formplugin.patrolscheme;

import kd.bos.base.BaseShowParameter;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.RowDataEntity;
import kd.bos.entity.datamodel.events.*;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.*;
import kd.bos.form.container.Tab;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.*;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.DateEdit;
import kd.bos.form.field.TextEdit;
import kd.bos.form.field.events.AfterBindingDataEvent;
import kd.bos.form.field.events.BasedataEditListener;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.id.ID;
import kd.bos.list.ListShowParameter;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import wens.pig.base.common.consts.BaseDataConst;
import wens.pig.base.common.consts.dhm.*;
import wens.pig.base.common.enums.BillStatusEnum;
import wens.pig.base.common.enums.EnableEnum;
import wens.pig.base.common.enums.YesOrNoEnum;
import wens.pig.base.utils.*;
import wens.pig.dhm.formplugin.envsamscheme.model.SchemeDetailThreeManyVO;
import wens.pig.dhm.util.SchemePatrolUtils;

import java.util.*;
import java.util.stream.Collectors;

import static wens.pig.base.common.consts.BaseDataConst.P_REFRESH;


/**
 * 严密闭方案(猪场)
 *  生产线的 keyRow = 月序号+LINENAME;
 * 非生产线的  keyRow = 月序号 + 二级分类的id
 * 父方案明细id  模拟ID组成 keyRow+三级分类 + 位点;
 * 二级分录的父父方案id为: SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_SUPUNLID).equals(keyRow)
 */
public class SchemePatrolPigFaFormPlugin extends AbstractFormPlugin implements RowClickEventListener,
        BeforeF7SelectListener, TabSelectListener,IDataModelChangeListener,BasedataEditListener {
        // BasedataEditListener,
    /**
     * 日志
     */
    private final static Log logger = LogFactory.getLog(SchemePatrolPigFaFormPlugin.class);
    /**
     * 保存操作
     */
    private final static String KEY_SAVE = "save";
    /**
     * 第一级单据体
     */
    private final static String KEY_ONEENTITY = "wens_entrymonth";
    /**
     * 第二级子单据体
     */
    private final static String ENTRYNLINE = "wens_entrynline";
    /**
     * 第二级子单据体,生产线
     */
    private final static String ENTRYPLINE = "wens_entrypline";
 
    /**
     * 第三级孙单据体
     */
    private final static String ENTRYTHREE = SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2;

    /**
     * 删除第二级行数据回调事件标识
     * 非生产线
     */
    private final static String DELNOLINE = "delnoline";

    /**
     * 删除第二级行数据回调事件标识
     * 生产线
     */
    private final static String DELLINE = "delline";
    /**
     * 关键字-用于构建缓存key  单据编号 唯一
     **/
    private static final String BILLNO = "billno";
    /**
     * cacheRegion 缓存区域--尽量对应应用名称
     **/
    private static final String CACHE_REGION = "pigSamSchemeCache";
    /**
     * oneToTwo 缓存区域--以二层数据为key存储所含三层数据number集合
     **/
    private static final String RELATION_TWO_TO_THREE = "twoToThreeQ";
 
    /**
     * 二级分录的页签
     */
    private static final String TAB = "wens_tabap";
    /**
     * 二级分录的页签
     * 生产线
     */
    private static final String TABLINE = "wens_tabpageap1";
    /**
     * 二级分录的页签
     * 非生产线
     */
    private static final String TABNLINE = "wens_tabpageap";

    /**
     * 第三级孙单据数据在缓存中的key
     **/
    private static final String THREEENTRYDATAIDS = "threeEntrydataIds";
    private static final String BASEDATA_FIELD = "wens_three2";
    private static final String CUSNAME = "wens_custom_name";
    private static final String NAME = "name";
    private static final String VERSIONNUM = "V1.0";
    private static final String LINENAME = "生产线";
    private static final String FILED = ".id";



    @Override
    public void beforeBindData(EventObject e) {
        BillShowParameter bill = (BillShowParameter) this.getView().getFormShowParameter();
        OperationStatus os = bill.getStatus();
        if(OperationStatus.ADDNEW.equals(os)){
            FormShowParameter showParameter = this.getView().getFormShowParameter();
            Object id = showParameter.getCustomParam(BaseDataConst.F_ID);
            if (null != id  ) {
                DynamicObject scheme = BusinessDataServiceHelper.loadSingle(id, SchemePatrolPigFaConstant.FORMBILLID);
                DynamicObject dynamicObject = this.getModel().getDataEntity(true);
                dynamicObject.set(SchemePatrolPigFaConstant.WENS_OLDID,  ToolUtils.cnulls(id));
                dynamicObject.set(SchemePatrolPigFaConstant.ORG, scheme.get(SchemePatrolPigFaConstant.ORG));
                dynamicObject.set(SchemePatrolPigFaConstant.WENS_UPDATE_STATUS, Boolean.TRUE);
                dynamicObject.set(SchemePatrolPigFaConstant.WENS_ONECLASS, scheme.getDynamicObject(SchemePatrolPigFaConstant.WENS_ONECLASS));
                //名称
//                dynamicObject.set(SchemePatrolPigFaConstant.WENS_NAME, scheme.get(SchemePatrolPigFaConstant.WENS_NAME));
                //上级方案
                dynamicObject.set(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME, scheme.get(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME));
                //引用方案
                DynamicObjectCollection quoteCol = (DynamicObjectCollection) scheme.get(SchemePatrolPigFaConstant.WENS_QUOTE_SCHEME);
                if(!CollectionUtils.isEmpty(quoteCol)){
                    MulBaseDataUtil.setMulBaseDataVal(dynamicObject,SchemePatrolPigFaConstant.WENS_QUOTE_SCHEME,quoteCol);
                }
                //到期前提醒天数
                dynamicObject.set(SchemePatrolPigFaConstant.WENS_REMINDDAYS, scheme.get(SchemePatrolPigFaConstant.WENS_REMINDDAYS));
                //是否最新方案
                dynamicObject.set(SchemePatrolPigFaConstant.WENS_ISNEW, YesOrNoEnum.YES.getValue());
                dynamicObject.set(SchemePatrolPigFaConstant.WENS_ENABLE, EnableEnum.DISABLE);

                SchemeViewUtil.copyEntryEntity(getView(), getModel(), SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL, scheme, null);
                SchemeViewUtil.copyEntryEntity(getView(), getModel(), SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH, scheme, null);
                //更新为最新的上级方案
                DynamicObject superior = updateTheSupScheme();
                //清空变更内容;
                clearChangeContent();
                if(null != superior){
                    compareSup (superior.getPkValue());
                    //最新方案为否,并且存在上级方案时
                }
            }
        }
        super.beforeBindData(e);
        //20240310 coyle_匡原志
        // 禁用状态、非最新方案，隐藏启用巡查和停用巡查按钮
        IDataModel model = this.getModel();
        DynamicObject object = model.getDataEntity(false);
        String isnew = ToolUtils.cnulls(object.getString("wens_isnew"));
        String enableVal = ToolUtils.cnulls(object.getString("wens_enable"));
        if(StringUtils.equals("0", isnew) && StringUtils.equals("0",enableVal)){
            this.getView().setVisible(false,  new String[]{"wens_startscheme","wens_stopscheme"});
        }
    }


    /**
     * @author  coyle
     * @param e
     */
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        BillShowParameter bill = (BillShowParameter) this.getView().getFormShowParameter();
        OperationStatus os = bill.getStatus();
        IDataModel model = this.getModel();
        String billStatus = ToolUtils.cnulls(model.getValue(SchemePatrolPigFaConstant.BILLSTATUS)) ;
        if(OperationStatus.ADDNEW.equals(os)){
            FormShowParameter showParameter = this.getView().getFormShowParameter();
            Object id = showParameter.getCustomParam(BaseDataConst.F_ID);
            if (null != id) {
                //是否使用
                model.setValue(SchemePatrolPigFaConstant.WENS_ENABLE, EnableEnum.DISABLE);
                //版本号
                model.setValue(SchemePatrolPigFaConstant.WENS_VERSIONNUM,"");
                //设为空,比较重要,以免重复对比生成其他数据
                showParameter.setCustomParam(BaseDataConst.F_ID,null);
                this.getView().setEnable(false,  "wens_oneclass");
            }else {
                //是否使用
                model.setValue(SchemePatrolPigFaConstant.WENS_ENABLE, EnableEnum.ENABLE);
                //版本号
                model.setValue(SchemePatrolPigFaConstant.WENS_VERSIONNUM,VERSIONNUM);
            }
            setDateVisble();
        }
        if(!StringUtils.equals(BillStatusEnum.AUDIT,billStatus)) {
            setToolVisible();
        }
        ///////////////////////////////////////////////////////////所有数据加入缓存//////////////////
        afterInitData();

    }

    /**
     * 日期隐藏
     */
    private void  setDateVisble() {
        this.getView().setVisible(false,  "wens_startdate" ,"wens_enddate");
    }
    private void  afterInitData(){
        this.getModel().deleteEntryData(ENTRYTHREE);
        String sheetName = ((Tab) this.getControl(TAB)).getCurrentTab();
        initShowDetailData(sheetName);
        setFildEnable();
        int tabIndex = getTabIndex();
        if(tabIndex == 1){
            setVisCol(TABLINE);
        }else{
            setVisCol(TABNLINE);
        }
    }


    /**
     * 清除变更内容
     */
    private  void  clearChangeContent(){
        DynamicObjectCollection entryEntity = this.getModel().getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        for(DynamicObject saveEntry: entryEntity){
            saveEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_CHANGES,"");
        }
        this.getModel().updateEntryCache(entryEntity);
    }


    /**
     * 更新上级方案
     */
    public  DynamicObject  updateTheSupScheme(){
        DynamicObject org = (DynamicObject) this.getModel().getValue(SchemePatrolPigFaConstant.ORG);
        DynamicObject oneClass = (DynamicObject) this.getModel().getValue(SchemePatrolPigFaConstant.WENS_ONECLASS);
        //查询上级组织
        if(null != oneClass && null != org) {
            Object orgId = org.getPkValue();
            DynamicObject supObj = SchemePatrolUtils.getParentScheme(orgId, oneClass);
            if (null != supObj) {
                this.getModel().setValue(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME, supObj);
                DynamicObjectCollection newCol = new DynamicObjectCollection();
                DynamicObjectCollection souCol = supObj.getDynamicObjectCollection(SchemePatrolMulConstant.WENS_ALL_SUP_SCHEME);
                for (DynamicObject obj : souCol) {
                    DynamicObject object = BusinessDataServiceHelper.loadSingle(obj.getString("fbasedataid.id"), SchemePatrolMulConstant.FORMBILLID);
                    newCol.add(object);
                }
                DynamicObject object = BusinessDataServiceHelper.loadSingle(supObj.getPkValue(), SchemePatrolMulConstant.FORMBILLID);
                newCol.add(object);
                //添加本身上级方案
//                this.getModel().setValue(SchemePatrolPigFaConstant.WENS_QUOTE_SCHEME, newCol);
                return supObj;
            }
        }
        this.getModel().setValue(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME, null);
        this.getModel().setValue(SchemePatrolPigFaConstant.WENS_QUOTE_SCHEME, null);
        return null;
    }



    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addItemClickListeners(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2 );
        // 引用方案
//        BasedataEdit supSchme = this.getControl(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME);
//        supSchme.addBeforeF7SelectListener(this);
        //生产线
        BasedataEdit lineCell = this.getControl(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_PROLINE);
        lineCell.addBeforeF7SelectListener(this);
        //猪舍
        BasedataEdit pigSty = this.getControl(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2);
        pigSty.addBeforeF7SelectListener(this);
        //三级分类
        BasedataEdit three = this.getControl(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2);
        three.addBeforeF7SelectListener(this);
        //巡查位点
        BasedataEdit site = this.getControl(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2);
        site .addBeforeF7SelectListener(this);
        //巡查组
        BasedataEdit group = this.getControl(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMGROUP2);
        group.addBeforeF7SelectListener(this);

        group = this.getControl(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO);
        group.addBeforeF7SelectListener(this);
        group = this.getControl(SchemePatrolPigFaConstant.WENS_ONECLASS);
        group.addBeforeF7SelectListener(this);
        BasedataEdit mulEdit = this.getControl(BASEDATA_FIELD);
        mulEdit.addBasedataEditListener(this);
        //分录
        EntryGrid nLine = this.getControl(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE  );
        nLine.addRowClickListener(this);
        EntryGrid pLine = this.getControl(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE  );
        pLine.addRowClickListener(this);
        EntryGrid oneEntry = this.getControl(KEY_ONEENTITY );
        oneEntry.addRowClickListener(this);
        // 监听选择tab 页签
        Tab  tabap = this.getControl(TAB);
        tabap.addTabSelectListener(this);
        //巡查明细工具栏
        this.addItemClickListeners("tbmain", "wens_advcontoolbarap2"," wens_entrymonth");
    }



    /**
     * 值改变事件
     * @param e
     */
    @Override
    public void propertyChanged(PropertyChangedArgs e) {

        String key = e.getProperty().getName();
        IDataModel model = this.getModel();
        ChangeData[] valueSet = e.getChangeSet();
        int rowIndex = valueSet[0].getRowIndex();
//        DynamicObjectCollection collection = getModel().getEntryEntity( ENTRYTHREE);
        DynamicObjectCollection collection = model.getDataEntity(true).getDynamicObjectCollection( ENTRYTHREE);
        switch (key) {
            //一级分类
            case SchemePatrolPigFaConstant.WENS_ONECLASS :
                DynamicObject newValueObj = (DynamicObject) valueSet[0].getNewValue();
                DynamicObject org = (DynamicObject) model.getValue(SchemePatrolPigFaConstant.ORG);
                clearAllData();
                //查询上级组织
                if (null == newValueObj || null == org ) {
                    return;
                }
                DynamicObject superior = updateTheSupScheme();
                if(null != superior){
                    insertSchemeVal(superior);
                    afterInitData();
                }
                setToolVisible();
                break;
            case SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME :
//                 newValueObj = (DynamicObject) valueSet[0].getNewValue();
//                setToolVisible();
//                if (null == newValueObj) {
//                    return;
//                }
//                clearAllData();
//                insertSchemeVal(newValueObj);
                break;
            //一级分录月份
            case SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_MONTH :
                String monthStr = ToolUtils.cnulls(e.getChangeSet()[0].getNewValue());
                if (StringUtils.isBlank(monthStr)) {
                    return;
                }
                filterMonthValue();
                break;
            //停用 生产线
            case SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_DEACTIVATE:
                DynamicObject pLineEntry = model .getEntryRowEntity(ENTRYPLINE , rowIndex);
                String markId = pLineEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);
                boolean newValue = (boolean) valueSet[0].getNewValue();
                updateTheStopSam( markId, newValue ) ;
                break;
            //停用 非生产线
            case SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_DEACTIVATE_NON:
                DynamicObject nLineEntry = model .getEntryRowEntity(ENTRYNLINE , rowIndex);
                markId = nLineEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID);
                newValue = (boolean) valueSet[0].getNewValue();
                updateTheStopSam( markId, newValue) ;
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2:
                //明细分类
                DynamicObject threeEntry = model .getEntryRowEntity(ENTRYTHREE , rowIndex);
                DynamicObject threeObj = (DynamicObject) valueSet[0].getNewValue();
                int tabIndex = getTabIndex();
                if(1 == tabIndex){
                    threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2, null);
                }else if(2 == tabIndex){
                    //20231012 提出不带出
//                    threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2 ,threeObj == null? "": threeObj.getString(NAME));
//                    threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2,"");
                    threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2, null);
                    model.updateEntryCache(collection);
                    this.getView().updateView(ENTRYTHREE);
                }
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2:
                //巡查位点
                threeEntry = model.getEntryRowEntity(ENTRYTHREE , rowIndex);
                DynamicObject siteObj = (DynamicObject) valueSet[0].getNewValue();
                //带出分类明细
                DynamicObject threeClass =   siteObj ==null ? null: siteObj.getDynamicObject(PatrolsiteConstant.GROUP);
                threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2,threeClass);
                //20231227
                if(null!= siteObj){
                    DynamicObjectCollection allEntryCol = model.getEntryEntity(ENTRYTHREE);
                    List<DynamicObject> enList = allEntryCol.stream().filter(theObj -> theObj.getBoolean("wens_ispresets2") &&
                            StringUtils.equals(ToolUtils.cnulls(theObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2 + ".id")),
                                    ToolUtils.cnulls(siteObj.getPkValue())))
                            .collect(Collectors.toList());
                    if(enList.size()>0){
                        DynamicObject object = enList.get(0);
                        threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22,
                                object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22));
                        threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2,
                                object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2));
                    }
                }
//                if(2== getTabIndex()){
                    //20231012 提出不带出
//                    threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2,siteObj == null? "":siteObj.getString(NAME));
//                    threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2,threeClass == null? "":threeClass.getString(NAME));
//                }
                model.updateEntryCache(collection);
                this.getView().updateView(ENTRYTHREE);
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22:
                //巡查周期
                threeEntry = model.getEntryRowEntity(ENTRYTHREE , rowIndex);
                String periodStr = (String) valueSet[0].getNewValue();
                if(!StringUtils.equals(periodStr,"1")){
                    threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2,periodStr);
                    model.updateEntryCache(collection);
                    this.getView().updateView(ENTRYTHREE);
                }
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_PROLINE:
                //
                newValueObj = (DynamicObject) valueSet[0].getNewValue();
                if(null != newValueObj){
                    pLineEntry = model .getEntryRowEntity(ENTRYPLINE , rowIndex);
                    String twocusid = pLineEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);

                    DynamicObjectCollection saveThreeCol = model.getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
                    //如果添加过,有预设值,则不进行更新了
                    List<DynamicObject> detailEntryCol = saveThreeCol.stream()
                            .filter(object -> object.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISPRESETS) == true
                                    && object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID).equals(twocusid) ).collect(Collectors.toList());
                    model.updateEntryCache(model.getEntryEntity(KEY_ONEENTITY));
                    model.updateEntryCache(model.getEntryEntity(ENTRYPLINE));
                    if(detailEntryCol.size() == 0){
                        if(null!= newValueObj){
//                        model.updateEntryCache(model.getEntryEntity(KEY_ONEENTITY));
//                        model.updateEntryCache(model.getEntryEntity(ENTRYPLINE));
                            this.getView().updateView(ENTRYPLINE);
                            int currentRow = model.getEntryCurrentRowIndex(KEY_ONEENTITY);
                            DynamicObject oneEntry = model.getEntryRowEntity(KEY_ONEENTITY, currentRow);
                            String  oneId = ToolUtils.cnulls(oneEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_SUPSEMONTHID));
                            fillLineToDetail( twocusid,oneId ,currentRow );
                            EntryGrid control = getControl(ENTRYPLINE);
                            control.selectRows(rowIndex);
                            pLineEntry = model .getEntryRowEntity(ENTRYPLINE , rowIndex);
                            String twoid = pLineEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);
                            model.deleteEntryData(ENTRYTHREE);
                            showDetailData(twoid);
                            setFildEnable();
                        }else{
                            //空值不删除202402
//                        model.deleteEntryData(ENTRYTHREE);
//                        deleteAllSavedEntry(twocusid,false);
                        }
                    }
                }
                //以下代码为了,在切换生产线时,清除猪舍的字段;
                DynamicObjectCollection detailEntry = model.getEntryEntity(ENTRYTHREE);
                DynamicObject oldVal = (DynamicObject) valueSet[0].getOldValue();
                String oldId = null == oldVal ? "" : oldVal.getString("id");
                String newId = null == newValueObj ? "" : newValueObj.getString("id");
                if( !StringUtils.equals(oldId,newId)) {
                    boolean flag = false;
                    for(int i=0;i<detailEntry.size();i++){
                        DynamicObject object = detailEntry.get(i);
                        object.set("wens_pigsty2",null);
                        three2ToThree(model,object);
                        flag = true;
                    }
                    if(flag){
                        this.getModel().updateEntryCache(detailEntry);
                        this.getView().updateView(ENTRYTHREE);
                    }
                }
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_STOPDETAIL2:
                setTwoEntryStop();
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO:
                //
                model.updateEntryCache(model.getEntryEntity(KEY_ONEENTITY));
                model.updateEntryCache(model.getEntryEntity(ENTRYNLINE));
                newValueObj = (DynamicObject) valueSet[0].getNewValue();
                //以下代码为了,在切换二级时,清除位点等的字段;
                detailEntry = model.getEntryEntity(ENTRYTHREE);
                oldVal = (DynamicObject) valueSet[0].getOldValue();
                oldId = null == oldVal ? "" : oldVal.getString("id");
                newId = null == newValueObj ? "" : newValueObj.getString("id");
                if( !StringUtils.equals(oldId,newId)) {
                    boolean flag = false;
                    for(int i=0;i<detailEntry.size();i++){
                        DynamicObject object = detailEntry.get(i);
                        object.set("wens_three2",null);
                        object.set("wens_site2",null);
                        three2ToThree(model,object);
                        flag = true;
                    }
                    if(flag){
                        this.getModel().updateEntryCache(detailEntry);
                        this.getView().updateView(ENTRYTHREE);
                    }
                }
                break;
            default:
                break;
        }
        //匹配变更字段
        if (StringUtils.equals( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2 , e.getProperty().getName())  ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant. WENS_ENTRYDETAIL2_WENS_SITEDETAILS2 , e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMGROUP2, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAILREMARK2, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_UPORG2, e.getProperty().getName()) ||
                StringUtils.equals(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_STOPDETAIL2, e.getProperty().getName())

        ){
            model.updateEntryCache(collection);
            this.getView().updateView(ENTRYTHREE);
            DynamicObject threeEntry = model.getEntryRowEntity(ENTRYTHREE , rowIndex);
//            threeEntry.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUUPDATE1,false);
//            DynamicObjectCollection savedEntryCol = model.getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
            three2ToThree(model,threeEntry);
            setFildEnable();
            if( (boolean)model.getValue(SchemePatrolPigFaConstant.WENS_UPDATE_STATUS) == true){
                colour("#FFFF00", false) ;
            }
        }
        super.propertyChanged(e);
    }

    //显示分录传数据到存储分录
    private  void  three2ToThree(IDataModel model  , DynamicObject  threeEntry){
        String entryId = threeEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2);
        DynamicObjectCollection savedEntryCol = model.getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        List<DynamicObject> collect = savedEntryCol.stream().filter(object -> StringUtils.equals(ToolUtils.cnulls(object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_CUSID)), entryId))
                .collect(Collectors.toList());
        boolean flag = false;
        for( DynamicObject saveObj :collect){
            updateNewEntry( threeEntry,saveObj);
            flag = true;
        }
        if(flag){
            model.updateEntryCache(model.getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL));
            getView().updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        }
    }


    /**
     * 设置二级分录停用
     */
    private  void  setTwoEntryStop(){
        IDataModel model = this.getModel();
        DynamicObjectCollection dynamicObjectCollection = model.getDataEntity(true).getDynamicObjectCollection(ENTRYTHREE);
        List<DynamicObject> collect = dynamicObjectCollection.stream().filter(object -> object.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_STOPDETAIL2) == false)
                .collect(Collectors.toList());

        int tabIndex1 = getTabIndex();
        if(tabIndex1 ==1){
            EntryGrid control = getControl(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE);
            int[] selectRows = control.getSelectRows();
            int selectRow = 0;
            if(selectRows.length>0) {
                selectRow = selectRows[0];
            }
            DynamicObject entryRowEntity = model.getEntryRowEntity(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE, selectRow);
            if(collect.size()>0) {
                entryRowEntity.set(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_DEACTIVATE,false);
            }else {
                entryRowEntity.set(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_DEACTIVATE,true);
            }
            model.updateEntryCache(model.getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH));
            model.updateEntryCache(model.getEntryEntity(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE));
//                this.getView().updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
            this.getView().updateView(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE);
            control.selectRows(selectRow);
        }else {
            EntryGrid control = getControl(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE);
            int[] selectRows = control.getSelectRows();
            int selectRow = 0;
            if (selectRows.length > 0) {
                selectRow = selectRows[0];
            }
            DynamicObject entryRowEntity = model.getEntryRowEntity(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE, selectRow);
            if (collect.size() > 0) {
                entryRowEntity.set(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_DEACTIVATE_NON, false);
            } else {
                entryRowEntity.set(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_DEACTIVATE_NON, true);
            }
            model.updateEntryCache(model.getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH));
            model.updateEntryCache(model.getEntryEntity(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE));
//                this.getView().updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
            this.getView().updateView(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE);
            control.selectRows(selectRow);
        }

    }

    /**
     * 填充生产线的数据到三级分录
     * 值改变事件
     */
    private  void  fillLineToDetail( String twocusid,String oneId,int  oneInd ){
        DynamicObjectCollection threeCol = getModel().getEntryEntity(ENTRYTHREE);
        DynamicObjectCollection saveThreeCol = getModel().getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        DynamicObject upSchemeObj =  getModel().getValue(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME) == null? null
                : (DynamicObject)getModel().getValue(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME);
        if(null!= upSchemeObj){
            upSchemeObj = BusinessDataServiceHelper.loadSingle(upSchemeObj.getPkValue(),SchemePatrolMulConstant.FORMBILLID);
            DynamicObjectCollection supMonthCol = upSchemeObj.getDynamicObjectCollection(SchemePatrolMulConstant.ENTRYENTITYID_WENS_PATROLM_MONTH);
            if(CollectionUtils.isEmpty(supMonthCol)){
                return;
            }
            List<DynamicObject> collect = supMonthCol.stream().filter(object -> object.getPkValue().toString().equals(oneId)).collect(Collectors.toList());
            DynamicObject mulObjOneEntry = SchemePatrolUtils.getMulObjOneEntry(upSchemeObj);
            if(collect.size()>0){
                mulObjOneEntry = collect.get(0);
            }
            if(null != mulObjOneEntry){
                String upOneId = mulObjOneEntry.getPkValue().toString();
                //获取到所有月份
                DynamicObjectCollection upTwoClassCol = upSchemeObj.getDynamicObjectCollection(SchemePatrolMulConstant.ENTRYENTITYID_WENS_PARTROLM_TWOEN);
                //获取到对应月份,所有的二级分录;
                List<DynamicObject> twoEntry = upTwoClassCol.stream().filter(object -> StringUtils.equals(object.getString(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_PARENT_ROWID),
                        upOneId)).collect(Collectors.toList());
                //获取二级分录中为生产线的数据;
                List<DynamicObject> lineEntry = twoEntry.stream().filter(object ->
                        StringUtils.equals(object.getString(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_SAMPLING_TWO+".name"),
                                "生产线")).collect(Collectors.toList());
                boolean flag = false;
                if(null != lineEntry && lineEntry.size()>0){
                    DynamicObject lineObj = lineEntry.get(0);
                    DynamicObjectCollection upDetailCol = lineObj.getDynamicObjectCollection(SchemePatrolMulConstant.SUBENTRYENTITYID_WENS_PATROLM_SUBEN);
                    for(DynamicObject upDetailObj:upDetailCol){
                        String keyRow = String.valueOf(oneInd)+LINENAME;
                        String threeId = addDetailRowDataFromSup (threeCol, upDetailObj, upSchemeObj, twocusid, keyRow, true);
                        addSaveRowData(  saveThreeCol ,  upDetailObj,  upSchemeObj,  twocusid ,threeId,true ,false);
                        flag = true;
                    }
                    if(flag){
                        this.getModel().updateEntryCache(saveThreeCol);
                    }
                }
            }
        }
    }

    /**
     * 新增巡查明细分录中数据
     * @param detailEntity  save分录集合
     * @param supDetailObj  上级方案\明细
     * @param supSchemeObj  上级方案对象
     * @param twocusid  本单据二级分录的id
     * @param isInit 是否预设;
     */
    private  void  addSaveRowData(DynamicObjectCollection detailEntity ,DynamicObject supDetailObj,DynamicObject supSchemeObj,String twocusid,
                                  String threecusid,boolean  isInit,boolean isUpdate){
        DynamicObject newDetailObj = new DynamicObject(detailEntity.getDynamicObjectType());
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_CUSID, threecusid  );
        DynamicObject threeObj = supDetailObj.getDynamicObject(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SAMPLING_THREE);
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_THREE,threeObj);
//        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PLACE,threeObj == null? "": threeObj.getString(NAME));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SITE,supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SITE));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SAMPERIOD2,supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_FREQUENCY));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_INTERVALDAY,supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_AVERAGE));
        if(null == supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SUPERIOR_ORG)){
            newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_UPORG,supSchemeObj.getDynamicObject("wens_org"));
        }else{
            newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_UPORG,supDetailObj.getDynamicObject(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SUPERIOR_ORG));
        }
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISPRESETS,isInit? true:false );
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID, twocusid);
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_SUPID, ToolUtils.cnulls(supDetailObj.getString("wens_cusid")));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUUPDATE ,isUpdate);
        detailEntity.add(newDetailObj);
    }

    /**
     * 过滤月份数据
     */
    private  void  filterMonthValue(){
        DynamicObjectCollection entryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
        HashSet<String> valSet = new HashSet();
        for( DynamicObject  entryObj :entryCol){
            String cnulls = ToolUtils.cnulls(entryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_MONTH));
            String[] split1 = cnulls.split(",");
            if(StringUtils.isNotBlank(isRepeatMonth(valSet, split1))){
                String repeatMonth = isRepeatMonth(valSet, split1);
                this.getView().showTipNotification("分录存在重复的月份<"+repeatMonth+">,请检查!",4000);
            }
        }
//        ComboEdit comboEdit = (ComboEdit)this.getControl("wens_month");
//        ComboProp property = (ComboProp)comboEdit.getProperty();
//        List<ValueMapItem> valueMapItem = property.getComboItems();
//        List<ComboItem> comboItems = new ArrayList<>();
//        for(ValueMapItem monthItem : valueMapItem){
//            ComboItem comboItem = new ComboItem();
//            comboItem.setCaption(monthItem.getName());
//            comboItem.setValue( monthItem.getValue());
//            comboItem.setItemVisible(true);
//            if(valSet.contains(monthItem.getValue())){
//                comboItem.setItemVisible(false);
//            }
//            comboItems.add(comboItem);
//        }
//        comboEdit.setComboItems(comboItems);
    }

    /**
     * 停用巡查,更新分录
     * @param newValue
     */
    private  void updateShowEntry(String parentId, boolean newValue){
        IDataModel model = this.getModel();
//        DynamicObjectCollection detailCol = model.getEntryEntity(ENTRYTHREE);
        DynamicObjectCollection detailCol = model.getDataEntity(true).getDynamicObjectCollection(ENTRYTHREE);
        detailCol.stream().filter(object -> object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PARENT_ROWID2)
                .equals(parentId)).forEach(object -> {
                    object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_STOPDETAIL2,newValue);
                }
        );
    }

    /**
     * 停用巡查,更新分录
     * @param newValue
     */
    private  void updateSaveEntry(String parentId, boolean newValue){
        IDataModel model = this.getModel();
//        DynamicObjectCollection detailCol = model.getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        DynamicObjectCollection detailCol = model.getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        detailCol.stream().filter(object -> object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID)
                .equals(parentId)).forEach(object -> {
            object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_STOPDETAIL,newValue);
        });

    }

    /**
     * 停用巡查,更新分录
     * @param newValue
     */
    private  void  updateTheStopSam(String parentId, boolean newValue){
        updateShowEntry(parentId,newValue);
        updateSaveEntry(parentId,newValue);
        DynamicObjectCollection collection = getModel().getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        model.updateEntryCache(collection);
//        view.updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
        collection = model.getEntryEntity(ENTRYTHREE);
        model.updateEntryCache(collection);
        view.updateView(ENTRYTHREE);
        collection = model.getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        model.updateEntryCache(collection);
        view.updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
    }

    /**
     * 获取当前激活的页签
     * @return
     */
    private int getTabIndex(){
        String sheetName = ((Tab) this.getControl(TAB)).getCurrentTab();
        if (TABNLINE.equals(sheetName)) {
            return 2;
        }else{
            return 1;
        }
    }



    /**
     * 判断是否有重复月
     * @param valSet
     * @param arr
     * @return
     */
    private  String  isRepeatMonth(HashSet<String> valSet, String[] arr){
        for(String monthVal : arr){
            if(!StringUtils.isBlank(monthVal)){
                if(valSet.contains(monthVal)){
                    return monthVal;
                }else{
                    valSet.add(monthVal);
                }
            }
        }
        return  "";
    }


    /**
     * 清理分录数据
     */
    private  void  clearAllData(){
        this.getModel().deleteEntryData(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
        this.getModel().deleteEntryData(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2);
        this.getModel().deleteEntryData(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
    }


    /**
     *  根据(多层级)方案,插入分录数据
     * @param schemeObj
     */
    private void  insertSchemeVal( DynamicObject schemeObj){
        Long schemeId = schemeObj.getLong("id");
        schemeObj = BusinessDataServiceHelper.loadSingle(schemeId, SchemePatrolMulConstant.FORMBILLID);
        DynamicObjectCollection  monthCol = schemeObj.getDynamicObjectCollection(SchemePatrolMulConstant.ENTRYENTITYID_WENS_PATROLM_MONTH );
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        // 复制分录信息
        DynamicObjectCollection monthEnEntity = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
        DynamicObjectCollection detailEntity = this.getModel().getEntryEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2);
        //一级单据
        int monthRow = 0;
        for(DynamicObject monthObj: monthCol){
            String month = monthObj.getString(SchemePatrolMulConstant.WENS_PATROLM_MONTH_WENS_MONTHS);
            DynamicObject newMonthObj = new DynamicObject(monthEnEntity.getDynamicObjectType());
//            long oneId = ID.genLongId();
            String oneId = String.valueOf(monthRow);
            newMonthObj.set(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_ONECUSID, oneId); //月份
            newMonthObj.set(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_MONTH, month); //月份
            newMonthObj.set(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_REMARK,monthObj.getString(SchemePatrolMulConstant.WENS_PATROLM_MONTH_WENS_REMARKS)); //备注
            newMonthObj.set(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_SUPSEMONTHID,  monthObj.getPkValue().toString()); //月份
            monthEnEntity.add(newMonthObj);
            DynamicObjectCollection  twotypeCol  = schemeObj.getDynamicObjectCollection(SchemePatrolMulConstant.ENTRYENTITYID_WENS_PARTROLM_TWOEN);
            DynamicObjectCollection entryLineCol = newMonthObj.getDynamicObjectCollection(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE);
            //二级
            for(DynamicObject twoTypeObj:  twotypeCol){
                //二级中如果是生产线的不要
                if(StringUtils.equals(twoTypeObj.getString(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_SAMPLING_TWO+".name"),
                        "生产线") ){
                    continue;
                }
                if (ToolUtils.cnulls(monthObj.get("id")).equals(ToolUtils.cnulls(twoTypeObj.get(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_PARENT_ROWID)))) {
                    DynamicObject newNlineObj = new DynamicObject(entryLineCol.getDynamicObjectType());
                    String  twocusid = monthRow + ToolUtils.cnulls(twoTypeObj.getString(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_SAMPLING_TWO + ".id"));
//                    String twocusid = String.valueOf(ID.genLongId()) ;
                    newNlineObj.set(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO, twoTypeObj.get(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_SAMPLING_TWO));
                    newNlineObj.set(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_DEACTIVATE_NON,  "0");
                    newNlineObj.set(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID, twocusid); //
                    newNlineObj.set( "wens_oneid", oneId );
                    newNlineObj.set( "wens_ispresets_nline", true);
                    newNlineObj.set(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_SUPUNLID ,twocusid);
                    DynamicObjectCollection supDetailCol = twoTypeObj.getDynamicObjectCollection(SchemePatrolMulConstant.SUBENTRYENTITYID_WENS_PATROLM_SUBEN);
                    //三级
                    for(DynamicObject supDetailObj : supDetailCol){
                        addDetailRowDataFromSup(  detailEntity ,  supDetailObj,  schemeObj, twocusid ,twocusid,true);
                    }
                    entryLineCol.add(newNlineObj);
                }
            }
            monthRow++;
        }
//
        getModel().updateCache();
        view.updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
        view.updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2);
        model.deleteEntryData( SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        addAllNewEntry();
    }


    /**
     * 新增巡查明细分录中数据_
     * 上级方案数据--展示分录;
     * @param detailEntity
     * @param supDetailObj
     * @param supSchemeObj
     * @param twocusid
     * @param isInit 是否预设;
     */
    private  String  addDetailRowDataFromSup (DynamicObjectCollection detailEntity ,DynamicObject supDetailObj,DynamicObject supSchemeObj,
                                      String twocusid,String  rowKey ,boolean  isInit){
        DynamicObject newDetailObj = new DynamicObject(detailEntity.getDynamicObjectType());
        DynamicObject threeObj = supDetailObj.getDynamicObject(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SAMPLING_THREE);
        String threeId = supDetailObj.getString(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SAMPLING_THREE+".id");
        String siteId = supDetailObj.getString(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SITE + ".id");
        //自定义id
        String  threecusid = ID.genStringId();
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2, threecusid  );
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2,threeObj);
//        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2,threeObj == null? "": threeObj.getString(NAME));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2,supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SITE));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22,supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_FREQUENCY));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2,supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_AVERAGE));
        if(null == supDetailObj.get(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SUPERIOR_ORG)){
            newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_UPORG2,supSchemeObj.getDynamicObject("wens_org"));
        }else{
            newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_UPORG2,supDetailObj.getDynamicObject(SchemePatrolMulConstant.WENS_PATROLM_SUBEN_WENS_SUPERIOR_ORG));
        }
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2,isInit? true:false );
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PARENT_ROWID2, twocusid);
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_SUPID1, ToolUtils.cnulls(supDetailObj.getString("wens_cusid")));
        newDetailObj.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUUPDATE1 ,false);
        detailEntity.add(newDetailObj);
        return String.valueOf(threecusid) ;
    }




    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        IDataModel model = this.getModel();
        String key = e.getProperty().getName();
        int row = e.getRow();
        ListShowParameter parameter = (ListShowParameter) e.getFormShowParameter();
        List<QFilter> filters = parameter.getListFilterParameter().getQFilters();
        switch (key) {
            case SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME:
                //this.getView().showConfirm("切换生产线将会清空分录的所有数据，是否继续变更?", MessageBoxOptions.YesNo, confirmCallBackListener) ;
                Object supscheme = model.getValue(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME);
                if(null != supscheme){
                    this.getView().showTipNotification("注意,切换引用方案将会清空与重置所有分录的数据!", 4000);
                }
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_PROLINE:
                //生产线
                DynamicObject orgObj = (DynamicObject) model.getValue(SchemePatrolPigFaConstant.ORG);
                int currentRow = model.getEntryCurrentRowIndex(KEY_ONEENTITY);
                DynamicObject oneEntry = model.getEntryRowEntity(KEY_ONEENTITY, currentRow);
                DynamicObjectCollection lineEntryCol = oneEntry.getDynamicObjectCollection(ENTRYPLINE);
                HashSet<String> idSet = new HashSet<>();
                for(DynamicObject lineEntryObj  :lineEntryCol) {
                    DynamicObject lineObj = lineEntryObj.getDynamicObject(EnvBatchSamConstant.WENS_LINEENTRY_WENS_PROLINE);
                    if(null!= lineObj){
                        idSet.add(lineObj.getPkValue().toString());
                    }
                }
                QFilter qFilter = QFilterUtils.getBaseDataQFilter();
                qFilter.and("wens_org", QCP.equals, orgObj.getPkValue() );
                if(idSet.size()>0){
                    qFilter.and("id", QCP.not_in, idSet);
                }
                filters.add(qFilter);

                break;
            case SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2 :
                int entryCurrentRowIndex = model.getEntryCurrentRowIndex(ENTRYPLINE);
                DynamicObject entryRowEntity = model.getEntryRowEntity(ENTRYPLINE, entryCurrentRowIndex);
                DynamicObject  proline = entryRowEntity.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_PROLINE);
                qFilter = QFilterUtils.getBaseDataQFilter();
                if(null == proline){
                    qFilter.and("id",QCP.equals,"abcd0000");
                }else{
                    qFilter.and("wens_produceline",QCP.equals,proline.getPkValue());
                }
                filters.add(qFilter);
                break;
            case SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2 :
                DynamicObject oneclass = model.getDataEntity(true).getDynamicObject(SchemePatrolPigFaConstant.WENS_ONECLASS);
                if(null == oneclass){
                    this.getView().showTipNotification("请先添加<一级区域分类>,再进行此操作!");
                    return;
                }
                qFilter = QFilterUtils.getBaseDataQFilter();
                if(1 == getTabIndex()){
                    //明细分类,只要环境的, 生产线下的
                    List<String> subClassData = SchemePatrolUtils.getTypeClassSubData4Line(
                             model.getDataEntity(true).getDynamicObject(SchemePatrolPigFaConstant.WENS_ONECLASS),"3");
                    if(null != subClassData){
                        List<String> lineClass = SchemePatrolUtils.getTypeClassSubData(
                                model.getDataEntity(true).getDynamicObject(SchemePatrolPigFaConstant.WENS_ONECLASS), "2", "生产线");
                        subClassData.addAll(lineClass);
                        qFilter.and(PatrolregionConstant.ID, QCP.in, subClassData  );
                    }else{
                        List<String> lineClass = SchemePatrolUtils.getTypeClassSubData(
                                model.getDataEntity(true).getDynamicObject(SchemePatrolPigFaConstant.WENS_ONECLASS), "2", "生产线");
                        if(null != lineClass){
                            qFilter.and(PatrolregionConstant.ID, QCP.in,lineClass) ;
                        }else{
                            qFilter.and(PatrolregionConstant.ID, QCP.equals,"000000") ;
                        }
                        //为空,则要2级
                    }
                }else  if(2 == getTabIndex()) {
                    //明细分类 非生产线
                    //判断是否有二级分类的值
                    EntryGrid oneEntryGrid = getControl(ENTRYNLINE);
                    int[] selectRows = oneEntryGrid.getSelectRows();
                    if (selectRows.length > 0) {
                        DynamicObject twoEntry = model.getEntryRowEntity(ENTRYNLINE, selectRows[0]);
                        DynamicObject classTwo = twoEntry.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO);
                        if (null != classTwo) {
                            //要找子节点分类 ,非生产线 //获取3级
                            List<String> subClassData = SchemePatrolUtils.getTypeClassSubData(classTwo,"3","");
                            if(null != subClassData){
                                subClassData.add(ToolUtils.cnulls(classTwo.getPkValue()));
                                qFilter.and(PatrolregionConstant.ID, QCP.in, subClassData);
                            }else{
                                //为空,则要2级
                                qFilter.and(PatrolregionConstant.ID, QCP.in, classTwo.getPkValue());
                            }
                        } else {
                            this.getView().showTipNotification("巡查二级分类为空,请先选择!", 4000);
                            qFilter.and(PatrolregionConstant.ID, QCP.equals, "null001");
                        }
                    }
                }
                filters.add(qFilter);
                break;
            case SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2:
                //巡查位点 //
                DynamicObject entryThree = model.getEntryRowEntity(ENTRYTHREE, row);
                qFilter = QFilterUtils.getBaseDataQFilter();
                DynamicObject threeObj = entryThree.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2);
                if( null != threeObj){
                    ArrayList<Long> subIdSet = new ArrayList<>();
                    SchemePatrolUtils.getSubClass(threeObj,subIdSet);
                    //加入自身
                    subIdSet.add((Long)threeObj.getPkValue());
                    qFilter.and(PatrolsiteConstant.GROUP,QCP.in,subIdSet);
                }else{
                    if(1 == getTabIndex()){
                        //生产线,
                        //只能取生产线下的
                        //获取所有生c线的数据
                        ArrayList<Long> detailIds = SchemePatrolUtils.getLineClassFilter(
                                ToolUtils.cnulls(model.getDataEntity(true).getString(SchemePatrolPigFaConstant.WENS_ONECLASS+FILED)),true);
                        qFilter.and(PatrolsiteConstant.GROUP,QCP.in,detailIds);
                        //排除已存在的
                        //可以直接根据界面的,不同的生产线可以重复;
//                    DynamicObjectCollection threeEntryCol = model.getDataEntity(true).getDynamicObjectCollection(ENTRYTHREE);
//                    List<String> collect = threeEntryCol.stream()
//                            .filter(object -> StringUtils.isNotBlank(object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2 + ".id")))
//                            .map(object -> object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2 + ".id")).collect(Collectors.toList());
                        //不去重
                        //qFilter.and(PatrolsiteConstant.ID, QCP.not_in, collect);
                    }else if(2 == getTabIndex() ){//非生产线 位点
                        int towIndex = model.getEntryCurrentRowIndex(ENTRYNLINE);
                        DynamicObject towEntry = model.getEntryRowEntity(ENTRYNLINE, towIndex);
                        DynamicObject towObj = towEntry.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO);
                        if(null!= towObj){
                            ArrayList<Long> subIdSet = new ArrayList<>();
                            SchemePatrolUtils.getSubClass(towObj,subIdSet);
                            subIdSet.add((Long)towObj.getPkValue());
                            qFilter.and("group",QCP.in,subIdSet);
                        }else {
                            qFilter.and(PatrolsiteConstant.ID,QCP.equals,"不给值");
                        }
                        //排除已存在的
                        //严格来说可以直接根据界面的值做判断;如树形结构特殊的话需要更加入对二级分录的判断;
//                        DynamicObjectCollection threeEntryCol = model.getDataEntity(true).getDynamicObjectCollection(ENTRYTHREE);
//                        List<String> collect = threeEntryCol.stream()
//                                .filter(object -> StringUtils.isNotBlank(object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2 + ".id")))
//                                .map(object -> object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2 + ".id")).collect(Collectors.toList());
                        //不去重
//                        qFilter.and(PatrolsiteConstant.ID, QCP.not_in, collect);
                    }
                }
                filters.add(qFilter);
                break;
            case  SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMGROUP2:
                //巡查组
                DynamicObject obj = (DynamicObject) model.getValue(SchemePatrolPigFaConstant.ORG);
                qFilter = QFilterUtils.getBaseDataQFilter();
                qFilter.and(PatrolTypeConstant.WENS_ORG, QCP.equals,obj.getPkValue());
                qFilter.and(PatrolTypeConstant.WENS_GTYPE, QCP.equals,"0");
                filters.add(qFilter);
                break;
            case SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO:
                qFilter = QFilterUtils.getBaseDataQFilter();
                qFilter.and("level",QCP.equals,"2");
                ArrayList<Long> pigFarmClass = SchemePatrolUtils.getPigFarmClass(
                        ToolUtils.cnulls(model.getDataEntity(true).getString(SchemePatrolPigFaConstant.WENS_ONECLASS+FILED)),false);
                qFilter.and(PatrolregionConstant.ID,QCP.in,pigFarmClass);
                //排除已有的
                orgObj = (DynamicObject) model.getValue(SchemePatrolPigFaConstant.ORG);
                currentRow = model.getEntryCurrentRowIndex(KEY_ONEENTITY);
                oneEntry = model.getEntryRowEntity(KEY_ONEENTITY, currentRow);
                DynamicObjectCollection entryEntity = oneEntry.getDynamicObjectCollection(ENTRYNLINE);
                ArrayList<String> ids = new ArrayList<>();
                for(DynamicObject twoEntry :entryEntity){
                    String twoObjId = twoEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO+".id");
                    if(StringUtils.isNotBlank(twoObjId)){
                        ids.add(twoObjId);
                    }
                }
                if(ids.size()>0){
                    qFilter.and(PatrolregionConstant.ID,QCP.not_in,ids);
                }
                filters.add(qFilter);
                break;
            case SchemePatrolPigFaConstant.WENS_ONECLASS:
                QFilter qFilters = QFilterUtils.getBaseDataQFilter();
                qFilters.and(new QFilter("level", QCP.equals, "1"));
                filters.add(qFilters);
                break;
            default:
                break;
        }

    }




    /**
     *操作前事件
     */
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate operate = (FormOperate) args.getSource();
        String operateKey = operate.getOperateKey();
        //新增分录时判断是否有勾选一级分录
        if(StringUtils.equals("newnoline",operateKey) || StringUtils.equals("newline",operateKey)){
            EntryGrid entryGrid = this.getControl(KEY_ONEENTITY);
            int[] selectRows = entryGrid.getSelectRows();
            if(selectRows.length == 0){
                DynamicObjectCollection entryEntity = this.getModel().getEntryEntity(KEY_ONEENTITY);
                if(entryEntity.size()>0){
                    entryGrid.selectRows(0);
                }else{
                    this.getView().showTipNotification("请先添加巡查月份,再进行此操作",3000);
                    args.setCancel(true);
                }
            }
        }
        else if(StringUtils.equals("newdetail",operateKey)){
            int currentRow = this.getModel().getEntryCurrentRowIndex(KEY_ONEENTITY);
            if(currentRow == -1){
                this.getView().showTipNotification("请先添加一级分录(巡查月份),再进行此操作",3000);
                args.setCancel(true);
                return;
            }
            DynamicObject oneEntry = this.getModel().getEntryRowEntity(KEY_ONEENTITY, currentRow);
            if(null == oneEntry){
                this.getView().showTipNotification("请先添加一级分录(巡查月份),再进行此操作",3000);
                args.setCancel(true);
                return;
            }
            String sheetName = ((Tab) this.getControl(TAB)).getCurrentTab();
            //非生产线
            if(TABNLINE.equals(sheetName)){
                EntryGrid entryGrid = this.getControl(ENTRYNLINE);
                int[] selectRows = entryGrid.getSelectRows();
                if(selectRows.length == 0){
                    DynamicObjectCollection entryEntity = oneEntry.getDynamicObjectCollection(ENTRYNLINE);
                    if(entryEntity.size()>0){
                        entryGrid.selectRows(0);
                    }else{
                        this.getView().showTipNotification("请先添加二级分录,再进行此操作",3000);
                        args.setCancel(true);
                    }
                }
            }else{
                EntryGrid entryGrid = this.getControl(ENTRYPLINE);
                int[] selectRows = entryGrid.getSelectRows();
                DynamicObjectCollection entryEntity = oneEntry.getDynamicObjectCollection(ENTRYPLINE);
                if(selectRows.length == 0){
                    if(entryEntity.size()>0){
                        entryGrid.selectRows(0);
                    }else{
                        this.getView().showTipNotification("请先添加二级分录,再进行此操作",3000);
                        args.setCancel(true);
                    }
                }
            }
        }else if(StringUtils.equals("newmonth",operateKey)){
            Object oneClass = getModel().getValue(SchemePatrolPigFaConstant.WENS_ONECLASS);
            if(null == oneClass){
                this.getView().showTipNotification("请先添加<一级区域分类>,再进行此操作",3000);
                args.setCancel(true);
                return;
            }
        }else  if(StringUtils.equals(operateKey,"audit")
                && StringUtils.equals(this.getModel().getDataEntity(true).getString(SchemePatrolPigFaConstant.WENS_VERSIONNUM),
                VERSIONNUM)) {
            String checkaudit = this.getView().getPageCache().get("checkaudit");
            if(ObjectUtil.isNull(checkaudit)){
                this.getView().getPageCache().put("checkaudit", "true");
                ConfirmCallBackListener  confirmCallBacks = new ConfirmCallBackListener("billaudit", this);
                this.getView().showConfirm("是否立刻生成巡查计划?", MessageBoxOptions.YesNo,
                        ConfirmTypes.Wait, confirmCallBacks);
                args.setCancel(true);
            }
        }
    }




    /**
     * 执行回调函数
     * 删除时提示
     */
    @Override
    public void confirmCallBack(MessageBoxClosedEvent messageBoxClosedEvent) {
        super.confirmCallBack(messageBoxClosedEvent);
        String callbackid = messageBoxClosedEvent.getCallBackId();
        if (StringUtils.equals("billaudit",callbackid)){
            //审核弹出框
            DynamicObject obj = this.getModel().getDataEntity(true);
            if(messageBoxClosedEvent.getResult() == MessageBoxResult.Yes){
                obj.set(SchemePatrolPigFaConstant.WENS_INADVANCE,true);
                SaveServiceHelper.update(obj);
                this.getView().invokeOperation("audit");
            }else if(messageBoxClosedEvent.getResult() == MessageBoxResult.No){
                obj.set(SchemePatrolPigFaConstant.WENS_INADVANCE,false);
                SaveServiceHelper.update(obj);
                this.getView().invokeOperation("audit");
            }
            this.getView().getPageCache().remove("checkaudit");
        }else  if (StringUtils.equals("stopscheme", callbackid)){
            //停用
            if(messageBoxClosedEvent.getResult() == MessageBoxResult.Yes){
                OperationResult stopscheme = this.getView().invokeOperation("stopscheme");
                if(stopscheme.isSuccess()){
                    this.getView().showSuccessNotification("操作成功!");
                    this.getView().updateView("wens_enddate");
                }
            }
        }
    }



    @Override
    public void afterAddRow(AfterAddRowEventArgs e) {
        super.afterAddRow(e);
        String keyName = e.getEntryProp().getName();
        IDataModel model = this.getModel();
        //巡查明细新增行
        if(ENTRYTHREE.equals(keyName)){
            String sheetName = ((Tab) this.getControl(TAB)).getCurrentTab();
            if(TABNLINE.equals(sheetName)){
                ///非生产线
                EntryGrid entryGrid = this.getControl(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE);
                int[] selectRows = entryGrid.getSelectRows();
                int selectRow = 0;
                if(selectRows.length>0){
                    selectRow = selectRows[0];
                }
                DynamicObject resEntry = this.getModel().getEntryRowEntity(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE , selectRow);
                if(null == resEntry){
                    return;
                }
                String cusId = resEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID);
                RowDataEntity[] rowdata=  e.getRowDataEntities();
                for (RowDataEntity rowDataEntity : rowdata) {
                    int currentindex=rowDataEntity.getRowIndex();
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PARENT_ROWID2, cusId,currentindex);
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2, ID.genLongId(),currentindex);
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISLINE1 , false,currentindex);
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2, false,currentindex );
                    //20231015
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUHAS1, false,currentindex );
//                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2, "" ,currentindex);
//                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2, "",currentindex );
                    //新增分录
                    DynamicObject  newEntry = model.getEntryRowEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2, currentindex);
                    addNewEntry(newEntry);

                }
            }else if(TABLINE.equals(sheetName)) {
                //生产线
                EntryGrid entryGrid = this.getControl(ENTRYPLINE);
                int[] selectRows = entryGrid.getSelectRows();
                int selectRow = 0;
                if(selectRows.length>0){
                    selectRow = selectRows[0];
                }
                DynamicObject resEntry = this.getModel().getEntryRowEntity(ENTRYPLINE , selectRow);
                if(null == resEntry){
                    return;
                }
                String cusId = resEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);
                RowDataEntity[] rowdata=  e.getRowDataEntities();
                for (RowDataEntity rowDataEntity : rowdata) {
                    int currentindex=rowDataEntity.getRowIndex();
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PARENT_ROWID2 , cusId,currentindex);
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2  , ID.genLongId(),currentindex);
                    model.setValue( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISLINE1 , true,currentindex);
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2, false ,currentindex);
                    //20231015
                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUHAS1, false,currentindex );
//                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2, "",currentindex );
//                    model.setValue(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2, "",currentindex );
                    //新增分录
                    DynamicObject  newEntry = model.getEntryRowEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2, currentindex);
                    addNewEntry(newEntry);
                }
//                model.updateEntryCache(model.getEntryEntity(ENTRYTHREE));
//                getView().updateView(ENTRYTHREE);
            }
            //202401 序号赋值;
            setSeqForEntry();
            // ||二级分录新增行
        }else if( SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE .equals(keyName)
                || SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE.equals(keyName)  ){
            ///非生产线分录
            EntryGrid entryGrid = this.getControl(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
            int[] selectRows = entryGrid.getSelectRows();
            if(selectRows.length>0){
                int selectRow = selectRows[0];
                DynamicObject resEntry = this.getModel().getEntryRowEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH , selectRow);
                String cusId = resEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_ONECUSID);
                RowDataEntity[] rowdata=  e.getRowDataEntities();
                setTwoEntryDeVal( rowdata,  keyName ,  cusId)   ;
            }else{
                DynamicObject resEntry = this.getModel().getEntryRowEntity(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH , 0);
                String cusId = resEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_ONECUSID);
                RowDataEntity[] rowdata=  e.getRowDataEntities();
                setTwoEntryDeVal( rowdata,  keyName ,  cusId)   ;
            }
            //获取当前单据id
            model.deleteEntryData(ENTRYTHREE);
        }
    }






    @Override
    public void afterCopyData(EventObject e) {
        super.afterCopyData(e);
        IDataModel model = this.getModel();
    }


    /**
     * 二级分录的默认值赋值
     * @param rowdata
     * @param keyName
     * @param cusId
     */
    private void  setTwoEntryDeVal(RowDataEntity[] rowdata,String keyName ,String cusId){
        for (RowDataEntity rowDataEntity : rowdata) {
//            DynamicObject row=rowDataEntity.getDataEntity();
            int currentindex=rowDataEntity.getRowIndex();
            if(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE.equals(keyName)){
                this.getModel().setValue("wens_oneid", cusId,currentindex);
                this.getModel().setValue(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID, ID.genLongId(),currentindex);
            }else{
                this.getModel().setValue("wens_lineoneid", cusId,currentindex);
                this.getModel().setValue(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID, ID.genLongId(),currentindex);
            }
        }
    }




    @Override
    public void beforeDeleteRow(BeforeDeleteRowEventArgs e) {
        super.beforeDeleteRow(e);
        int[] rowIndex = e.getRowIndexs();
        //获取当前单据id
        String billId = this.getModel().getValue(BILLNO).toString();
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        String name = e.getEntryProp().getName();
        if(StringUtils.equals(ENTRYTHREE,name )){
//            EntryGrid entryGrid = this.getControl(ENTRYTHREE);
//            int[] selectRows = entryGrid.getSelectRows();
            List<Integer>  indexList = new ArrayList<Integer>();
            if(rowIndex.length>0) {
                for (int currentRow : rowIndex) {
                    DynamicObject twoEntry = this.getModel().getEntryRowEntity(ENTRYTHREE, currentRow);
                    boolean isPresets = twoEntry.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2);
                    if (isPresets) {
                        indexList.add(currentRow + 1);
                    }
                }
                if (indexList.size() > 0) {
                    this.getView().showTipNotification("第" + ToolUtils.aryToStr(indexList, false) + " 行分录来自引用方案，无法删除,请先取消选择!",3000 );
                    e.setCancel(true);
                }else{
                    for (int currentRow : rowIndex) {
                        DynamicObject twoEntry = this.getModel().getEntryRowEntity(ENTRYTHREE, currentRow);
                        String remarkId = ToolUtils.cnulls(twoEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2));
                        //删除三级分录
                        deleteSavedEntry(remarkId);
                    }
                }

            }
        }else if(StringUtils.equals(ENTRYNLINE,name )){
            //非生产线分录
            List<Integer>  indexList = new ArrayList<Integer>();
            if(rowIndex.length>0){
                for(int currentRow: rowIndex){
                    DynamicObject twoEntry = this.getModel().getEntryRowEntity(ENTRYNLINE , currentRow);
                    boolean isPresets = twoEntry.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_ISPRESETS_NLINE );
                    if(isPresets){
                        indexList.add(currentRow+1);
                    }
                }
                if(indexList.size()>0){
                    this.getView().showTipNotification("第" +ToolUtils.aryToStr(indexList,false) + " 行分录来自引用方案，无法删除,请先取消选择!;");
                    e.setCancel(true);
                }
                //引用方案不包含其中
                if(rowIndex.length>0 && indexList.size() ==0){
                    for(int currentRow: rowIndex){
                        DynamicObject twoEntry = this.getModel().getEntryRowEntity(ENTRYNLINE , currentRow);
                        String remarkId = twoEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID);
                        deleteAllSavedEntry(remarkId,false);
                    }
                    //清空对应巡查明细数据
                    this.getModel().deleteEntryData(ENTRYTHREE);
                }
            }
        }else if(StringUtils.equals(ENTRYPLINE,name )){
            //生产线分录
            List<Integer>  indexList = new ArrayList<Integer>();
            if(rowIndex.length>0){
                for(int currentRow: rowIndex){
                    DynamicObject twoEntry = this.getModel().getEntryRowEntity(ENTRYPLINE , currentRow);
                    boolean isPresets = twoEntry.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_ISPRESETS_LINE);
                    if(isPresets){
                        indexList.add(currentRow+1);
                    }
                }
                if(indexList.size()>0){
                    this.getView().showTipNotification("第" +ToolUtils.aryToStr(indexList,false) + " 行分录来自引用方案，无法删除,请先取消选择!",3000);
                    e.setCancel(true);
                }
                //引用方案不包含其中
                if(rowIndex.length>0 && indexList.size() == 0){
                    for(int currentRow: rowIndex){
                        DynamicObject twoEntry = this.getModel().getEntryRowEntity(ENTRYPLINE , currentRow);
                        String remarkId = twoEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);
                        deleteAllSavedEntry(remarkId,false);
                    }
                    //清空对应巡查明细数据
                    this.getModel().deleteEntryData(ENTRYTHREE);
                }
            }
        }
    }




    /**
     * 删除数据分录中的数据
     * @param perentId
     * @param delDef  只删预设数据;
     */
    private  void  deleteAllSavedEntry(String perentId, boolean delDef){
        IDataModel model = this.getModel();
        DynamicObjectCollection newEntryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        DynamicObjectCollection dynamicObjects = new DynamicObjectCollection();
        for(DynamicObject newEntry:  newEntryCol ){
            if( !StringUtils.equals(ToolUtils.cnulls(newEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID)),perentId)){
                //只删预设数据;
                if(delDef){
                    if(newEntry.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISPRESETS) == false){
                        //非预设的添加进集合;
                        dynamicObjects.add(newEntry);
                    }
                }else{
                    dynamicObjects.add(newEntry);
                }
            }
        }
        model.deleteEntryData(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        newEntryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        for( DynamicObject newEntry: dynamicObjects){
            newEntryCol.add(newEntry);
        }
        model.setValue(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL,newEntryCol);
        this.getModel().updateEntryCache(newEntryCol);
//        this.getView().updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
    }

    /**
     * 删除数据分录中的数据
     * @param remarkId
     */
    private  void  deleteSavedEntry(String  remarkId){
        IDataModel model = this.getModel();
        DynamicObjectCollection newEntryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        DynamicObjectCollection dynamicObjects = new DynamicObjectCollection();
        for(DynamicObject newEntry:  newEntryCol ){
            if( !StringUtils.equals(ToolUtils.cnulls(newEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_CUSID)),remarkId)){
                dynamicObjects.add(newEntry);
            }
        }
        model.deleteEntryData(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        newEntryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        for( DynamicObject newEntry: dynamicObjects){
            newEntryCol.add(newEntry);
        }
        model.setValue(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL,newEntryCol);
        this.getModel().updateEntryCache(newEntryCol);
        this.getView().updateView(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
    }

    @Override
    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        String itemKey = evt.getItemKey();
        if(StringUtils.equals(itemKey,"wens_stopscheme")){
            DynamicObject dataEntity = this.getModel().getDataEntity(true);
            if(!StringUtils.equals(dataEntity.getString("wens_enable"),YesOrNoEnum.YES.getValue())){
                this.getView().showTipNotification("此方案不为启用方案,无需进行停用!");
                return ;
            }
            if(StringUtils.equals(dataEntity.getString("wens_enable"),"2")){
                this.getView().showTipNotification("此方案为停用方案,无需再次进行停用操作!");
                return ;
            }
            ConfirmCallBackListener confirmCallBacks = new ConfirmCallBackListener("stopscheme", this);
            this.getView().showConfirm("是否确认停用此条方案?", MessageBoxOptions.YesNo,
                    ConfirmTypes.Default, confirmCallBacks);
        }else  if(StringUtils.equals(itemKey,"wens_startscheme")){
            DynamicObject dataEntity = this.getModel().getDataEntity(true);
            if(!StringUtils.equals(dataEntity.getString(SchemePatrolPigFaConstant.WENS_ENABLE),"2")){
                this.getView().showTipNotification("此方案不为停用方案,无需进行此操作!");
            }else{
                BaseShowParameter showParameter = new BaseShowParameter();
                showParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                showParameter.setFormId(SchemePatrolPigFaConstant.FORMBILLID);
                showParameter.setStatus(OperationStatus.ADDNEW);
                showParameter.setCustomParam(BaseDataConst.F_ID, dataEntity.getPkValue());
                showParameter.setCloseCallBack(new CloseCallBack(this, SchemePatrolPigFaConstant.FORMBILLID));
                this.getView().showForm(showParameter);
            }
        }
    }



    /**
     * 初始化显示三级分录
     * 数据
     */
    private  void initShowDetailData(String tabKey){
        EntryGrid oneGrid = this.getView().getControl(KEY_ONEENTITY);
        int[] oneRows = oneGrid.getSelectRows();
        int currentRowOne = 0;
        if(oneRows.length == 0){
            oneGrid.selectRows(currentRowOne);
        }else{
            currentRowOne = oneRows[0];
        }
        DynamicObject oneEntry = this.getModel().getEntryRowEntity(KEY_ONEENTITY, currentRowOne);

        //清空点击前数据
        this.getModel().deleteEntryData(ENTRYTHREE);
        //非生产线
        if (StringUtils.equals(tabKey, TABNLINE)) {
            this.getView().setVisible(false,new String[]{
                    SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2,
                    SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2
            });
        }else{
            this.getView().setVisible(true,new String[]{
                    SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2,
                    SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2
            });
        }
        //以下为点击页签时,根据第一条父分录,自动显示明细分录上的内容非生产线 ,体验不好,先不使用,看反馈;
        if (StringUtils.equals(tabKey, TABNLINE)) {
            if(null!=oneEntry){
                DynamicObjectCollection twoEntryData =  oneEntry.getDynamicObjectCollection(ENTRYNLINE) ;
                if(twoEntryData.size()>0){
                    EntryGrid nlineGrid = this.getView().getControl(ENTRYNLINE);
                    int[] nlineRows = nlineGrid.getSelectRows();
                    int twoIndex = 0;
                    if(nlineRows.length > 0){
                        twoIndex = nlineRows[0];
                    }
                    DynamicObject twoEntry = this.getView().getModel().getEntryRowEntity(ENTRYNLINE, twoIndex);
                    if (null != twoEntry) {
                        String cusId = twoEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID);
                        showDetailData(cusId);
                    }
                }
            }
        }else  if (StringUtils.equals(tabKey, TABLINE)) {
            //生产线
            if(null != oneEntry){
                DynamicObjectCollection twoEntryData =  oneEntry.getDynamicObjectCollection(ENTRYPLINE) ;
                if(twoEntryData.size()>0){
                    EntryGrid lineGrid = this.getView().getControl(ENTRYPLINE);
                    int[] nlineRows = lineGrid.getSelectRows();
                    int twoIndex = 0;
                    if(nlineRows.length > 0){
                        twoIndex = nlineRows[0];
                    }
                    DynamicObject twoEntry = this.getModel().getEntryRowEntity(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE, twoIndex);
                    if (null != twoEntry) {
                        String twoCusId = twoEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);
                        showDetailData(twoCusId);
                    }
                }
            }
        }
    }


    @Override
    public void tabSelected(TabSelectEvent arg) {
        String tabKey = arg.getTabKey();
        //数据加入缓存
//        addThreeDataToCache();
        //显示三级分录的内容
        initShowDetailData(  tabKey);
        //界面列显示
        setVisCol(tabKey);
        setFildEnable();
    }

    /**
     * 调整界面显示
     */
    private  void  setVisCol(String tabKey){
        IFormView view = this.getView();
        //非生产线
        if (StringUtils.equals(tabKey, TABNLINE)) {
            setMustInput(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2,true);
            view.setVisible(false, new String[] {"wens_pigsty"});
        }else{
            view.setVisible(true, new String[] {"wens_pigsty"});
            setMustInput(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2,true);
            setMustInput(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2,true);
        }
    }




    /**
     * @Description 分录行点击事件
     * @Param [evt]
     * @return void
     *  单据体焦点行、焦点单元格切换时，点击鼠标，切换行、单元格时触发此事件：
     * 按键盘上下左右键切换单元格，不触发事件，但是开始编辑单元格值，触发事件
     **/
    @Override
    public void entryRowClick(RowClickEvent evt) {
        EntryGrid entryGrid = (EntryGrid) evt.getSource();
        String entryKey = entryGrid.getEntryKey();
        if(StringUtils.equals(KEY_ONEENTITY, entryKey)){
            filterMonthValue();
        }
        RowClickEventListener.super.entryRowClick(evt);
        if (StringUtils.equals(ENTRYNLINE, entryKey) || StringUtils.equals(ENTRYPLINE, entryKey)
                || StringUtils.equals(KEY_ONEENTITY, entryKey)) {
            int rowNum = evt.getRow();
            //非取消点击行 //获取当前单据id
            if (rowNum >= 0) {
                //清空点击前数据
                this.getModel().deleteEntryData(ENTRYTHREE);
                if (StringUtils.equals(ENTRYNLINE, entryKey)) {
                    EntryGrid grid = this.getView().getControl(ENTRYNLINE);
                    int[] selectrows = grid.getSelectRows();
                    //遍历所有勾选的行
                    for (int rowNumber : selectrows) {
                        //获取当前子单据体对象
                        DynamicObject dynamicObject = this.getModel().getEntryRowEntity(ENTRYNLINE, rowNumber);
                        String rowCusId = dynamicObject.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID);
                        showDetailData(rowCusId);
                    }
                } else if (StringUtils.equals(ENTRYPLINE, entryKey)){
                    EntryGrid grid = this.getView().getControl(ENTRYPLINE);
                    int[] selectrows = grid.getSelectRows();
                    //遍历所有勾选的行
                    for (int rowNumber : selectrows) {
                        //获取当前子单据体对象
                        DynamicObject dynamicObject = this.getModel().getEntryRowEntity(ENTRYPLINE, rowNumber);
                        String rowCusId = dynamicObject.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);
                        showDetailData(rowCusId);
                    }
                }
                //优化项,点击一级分录时,显示三级分录的数据,
                else if (StringUtils.equals(KEY_ONEENTITY, entryKey)) {
                    String sheetName = ((Tab) this.getControl(TAB)).getCurrentTab();
                    initShowDetailData(sheetName);
                    BillShowParameter bill = (BillShowParameter) this.getView().getFormShowParameter();
                    OperationStatus os = bill.getStatus();
                    if(OperationStatus.EDIT.equals(os) && !StringUtils.equals(ToolUtils.cnulls(this.getModel().getValue(SamQueryConstant.BILLSTATUS) ),BillStatusEnum.SAVE)){
                        selectedTwoEntryRow(); //体验非常不好,会跳,编辑时不用
                    }
                    if( (boolean)this.getModel().getValue(SchemePatrolPigFaConstant.WENS_UPDATE_STATUS) == true){
                        colour("#FFFF00", false) ;
                    }
                }
            }
            setFildEnable();
        }
    }



    /**
     * 默认选中二级分录第一行
     */
    private  void  selectedTwoEntryRow(){
        int tabIndex = getTabIndex();
        EntryGrid one = this.getControl(KEY_ONEENTITY);
        int[] selectRows = one.getSelectRows();
        if(tabIndex == 1 ){
            if(selectRows.length>0){
                DynamicObject entity = this.getModel().getEntryRowEntity(KEY_ONEENTITY, selectRows[0]);
                DynamicObjectCollection twoCol = entity.getDynamicObjectCollection(ENTRYPLINE);
                if(twoCol.size()>0){
                    EntryGrid two = this.getControl(ENTRYPLINE);
                    two.selectRows(0);
                }
            }
        }else{
            if(selectRows.length>0){
                DynamicObject entity = this.getModel().getEntryRowEntity(KEY_ONEENTITY, selectRows[0]);
                DynamicObjectCollection twoCol = entity.getDynamicObjectCollection(ENTRYNLINE);
                if(twoCol.size()>0){
                    EntryGrid two = this.getControl(ENTRYNLINE);
                    two.selectRows(0);
                }
            }
        }
    }




    /***
     * 三级分录增加数据时调用
     * 将目前三级分录上所展示的数据保存到(隐藏的保存分录)
     * 用于存到保存分录
     */
    private void addAllNewEntry(){
        DynamicObjectCollection showEntryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2);
        for(DynamicObject showEntry: showEntryCol){
            addNewEntry(showEntry);
        }
    }


    /**
     * 控件隐藏
     */
    private  void  setToolVisible(){
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        DynamicObject value = (DynamicObject)model.getValue(SchemePatrolPigFaConstant.WENS_SUPERIOR_SCHEME);
        //巡查月份的分录按钮
        if(null != value){
            DynamicObjectCollection monthEntry = model.getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
            view.setVisible(false,  "wens_advcontoolbarap");
//            月份锁定
            int ii = 0;
            for(DynamicObject  month: monthEntry){
                view.setEnable(false,ii,new String[]{ SchemePatrolPigFaConstant.WENS_ENTRYMONTH_WENS_MONTH});
                ii++;
                //202402 二级分类定
                DynamicObjectCollection nlineEnCol = month.getDynamicObjectCollection(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE);
                int jj = 0;
                for(DynamicObject  nLineEn: nlineEnCol){
                    boolean isPresets = nLineEn.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_ISPRESETS_NLINE );
                    if(isPresets){
                        view.setEnable(false,jj,new String[]{ SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO});
                    }
                    jj++;
                }

            }
        }else{
            view.setVisible(true,  "wens_advcontoolbarap");
        }
    }


    /**
     *数据对比
     * @param supSchemeId
     */
    private  void  compareSup(Object supSchemeId ) {
        //父类方案
        DynamicObject   supScheme = BusinessDataServiceHelper.loadSingle(supSchemeId, SchemePatrolMulConstant.FORMBILLID);
        DynamicObjectCollection supMonthCol = supScheme.getDynamicObjectCollection(SchemePatrolMulConstant.ENTRYENTITYID_WENS_PATROLM_MONTH);
        IDataModel model = this.getModel();
        // 复制分录信息
        DynamicObjectCollection monthEnCol = model.getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYMONTH);
        //如果删除,需加入判断预设数据 ,清楚上级组织,并取消预设;初始化为上级不存在
        DynamicObjectCollection detailCol = getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        //上级存在与是否更新都改为否,初始化
        detailCol.stream().forEach(
                object-> {
                    object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUHAS,false);
                    object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUUPDATE, false);
                }
        );
        //一级单据
        for(int i = 0; i< supMonthCol.size();i++ ){
            DynamicObject supMonthObj = supMonthCol.get(i);
            String supmoId = supMonthObj.getPkValue().toString();
            if(monthEnCol.size() > i){
                //本方案中的月份;
                DynamicObject monObj = monthEnCol.get(i);
                compareAllData(i,  supmoId,  supScheme,  monObj  );
            }
            //拿到月份
        }

        //如果删除,需加入判断预设数据 ,清楚上级组织,并取消预设;
        detailCol.stream().filter(object -> object.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUHAS) == false
                && object.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISPRESETS) == true ).forEach(
                object-> {
                    object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_UPORG,null);
                    object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISPRESETS,false);
                    object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUUPDATE, true);
                    object.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_CHANGES,"上级方案已删除此位点");
                }
        );
    }


    /**
     *
     * @param supMonthId  上级方案中,月分录中的id
     * @param supScheme  上级方案
     * @param monObj  月分录id
     * 对比非生产线
     */
    private void  compareAllData( int monthInd, String supMonthId,DynamicObject supScheme,DynamicObject monObj) {
        //根据month找到所有父类的非生产线,
        //这个集合是父类中的二级分录数据,也就是 二级分类;
        DynamicObjectCollection supTwoCol = supScheme.getDynamicObjectCollection(SchemePatrolMulConstant.ENTRYENTITYID_WENS_PARTROLM_TWOEN);
        for (DynamicObject supTow : supTwoCol) {
            //根据父方案一级分录中的数据拿到二级分类的数据,进行操作
            if (supMonthId.equals(ToolUtils.cnulls(supTow.getString(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_PARENT_ROWID)))) {
                String twoClassName = ToolUtils.cnulls(supTow.getString(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_SAMPLING_TWO+".name"));
                if(StringUtils.equals(twoClassName, LINENAME)){
                    //生产线上;
                    compareLineDetailData( monthInd, supScheme,  supTow,  monObj);
                }else{
                    //非生产线中明细进行对比
                    compareDetailData( monthInd, supScheme,  supTow,  monObj);
                }
            }
        }

    }


    /**
     * 对比巡查明细
     * @param supScheme 上级方案
     * @param supTow  上级方案的二级分类
     * @param monObj 上级方案的一级分录;
     */
    private   void  compareLineDetailData(int monthInd, DynamicObject supScheme,DynamicObject supTow,DynamicObject monObj){
        DynamicObjectCollection detailCol = getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
//        String supTowId = supTow.getPkValue().toString();
        //父类的三级分录
        DynamicObjectCollection supDetailCol = supTow.getDynamicObjectCollection(SchemePatrolMulConstant.SUBENTRYENTITYID_WENS_PATROLM_SUBEN);
        for (DynamicObject supDetail : supDetailCol) {
            boolean isNew = true;
            String supThreeId = ToolUtils.cnulls( supDetail.getString("wens_cusid")) ;
            //循环对比
            for (DynamicObject detail : detailCol) {
                String threeSupId = ToolUtils.cnulls(detail.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_SUPID));
                if (StringUtils.equals(supThreeId, threeSupId)) {
                    //如果修改
                    if (SchemePatrolUtils.contrastViewData(detail, supDetail)) {
                        detail.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUUPDATE, true);
                    }
                    //上级存在
                    detail.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUHAS, true);
                    isNew = false;
                    continue;
                }
            }
            //新增
            if (isNew) {
                //需要月份;
                DynamicObjectCollection lineObjCol = monObj.getDynamicObjectCollection(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYPLINE);
                for(DynamicObject lineObj : lineObjCol ){
                    String twoId = lineObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYPLINE_WENS_LINECACHEID);
                    String threeId = ID.genStringId();
                    addSaveRowData(  detailCol ,  supDetail,  supScheme,  twoId,threeId,true,true);
                }
            }
        }
    }




    /**
     * 对比巡查明细 非生产线中明细进行对比
     * @param supScheme 上级方案
     * @param supTow  上级方案的二级分类
     * @param monObj 上级方案的一级分录;
     */
    private   void  compareDetailData(int monthInd, DynamicObject supScheme,DynamicObject supTow,DynamicObject monObj){
        DynamicObjectCollection detailCol = getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        String supTowId = supTow.getPkValue().toString();
        //父类的三级分录
        DynamicObjectCollection supDetailCol = supTow.getDynamicObjectCollection(SchemePatrolMulConstant.SUBENTRYENTITYID_WENS_PATROLM_SUBEN);
        for (DynamicObject supDetail : supDetailCol) {
            boolean isNew = true;
            String supThreeId = ToolUtils.cnulls(supDetail.getString("wens_cusid")) ;
            //循环对比
            for (DynamicObject detail : detailCol) {
                String threeSupId = ToolUtils.cnulls(detail.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_SUPID));
                if (StringUtils.equals(supThreeId, threeSupId)) {
                    //如果修改
                    if (SchemePatrolUtils.contrastViewData(detail, supDetail)) {
                        detail.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUUPDATE, true);
                    }
                    //上级存在
                    detail.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUHAS, true);
                    isNew = false;
                    continue;
                }
            }
            //新增
            if (isNew) {
                String twoClassId = supTow.getString(SchemePatrolMulConstant.WENS_PARTROLM_TWOEN_WENS_SAMPLING_TWO+".id");
                //本方案中数据;
                DynamicObjectCollection nlineObj = monObj.getDynamicObjectCollection(SchemePatrolPigFaConstant.SUBENTRYENTITYID_WENS_ENTRYNLINE);
                nlineObj.stream().filter(object -> object.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CLASSTWO+".id").equals(twoClassId))
                        .forEach(object->{
                            String twoId = object.getString(SchemePatrolPigFaConstant.WENS_ENTRYNLINE_WENS_CACHEID);
//                            String threeId = addDetailRowData(detailCol, supDetail, supScheme, twoId, true);
                            String threeId = ID.genStringId();
                            addSaveRowData(  detailCol ,  supDetail,  supScheme, twoId,threeId,true,true);
                        });
            }
        }
    }



    /**
     * 修改分录颜色
     * @param color 颜色
     * @param str 是否锁定
     */
    private void colour( String color, boolean str) {
        DynamicObjectCollection threeEntryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(ENTRYTHREE);
//        List<Integer> collect = entity.stream().filter(info -> info.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUUPDATE)==true).
//                map(detail -> detail.getInt(BaseDataConst.F_SEQ)).collect(Collectors.toList());
        int i = 0;
        ArrayList<Integer> seqList = new ArrayList<Integer>();
        for(DynamicObject  obj : threeEntryCol){
            if(obj.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUUPDATE1)){
                seqList.add(i);
            }
            i++;
        }
        int index = 0;
        int[] a = new int[seqList.size()];
        for (Integer integer : seqList) {
            a[index] = integer;
            index++;
        }
        EntryGrid control = this.getView().getControl(ENTRYTHREE);
        control.setRowBackcolor(color, a );
        control.setRowLock(str, a);
    }





    /**
     * 反解析选择的数据，并设置过滤条件
     * @param e
     */
    @Override
    public void beforeSetItemValue(BeforeSetItemValueEventArgs e) {
        Object value = e.getValue();
        if (value == null) {
            return;
        }
        String searchKey = null;
        IDataEntityProperty property = e.getProperty();
        if (BASEDATA_FIELD.equals(property.getName())) {
            searchKey = CUSNAME;
        }
        if (StringUtils.isBlank(searchKey)) {
            return;
        }
        String[] split = value.toString().split(",|;");
        String[] qfliterArr = new String[split.length];
        for (int i = 0; i < split.length; i++) {
            qfliterArr[i] = split[i].split("[( )]")[0];
        }

        //设置查询的字段
        e.setSearchKey(searchKey);
        //设置查询的数据
        e.setSearchArgs(qfliterArr);
    }

    /**
     *分录数据新增
     * 到保存分录
     */
    private  void  addNewEntry(DynamicObject showObj){
        DynamicObjectCollection saveCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        DynamicObject objRes = new DynamicObject(saveCol.getDynamicObjectType());
        //识别id:分录id+创建时间点
        updateNewEntry(showObj,objRes);
        saveCol.add(objRes);
        this.getModel().updateEntryCache(saveCol);
    }




    /**
     *分录数据新增
     * 到保存分录
     */
    private  void  updateNewEntry(DynamicObject showObj,DynamicObject objRes){
        objRes.set(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID, showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PARENT_ROWID2 ) );
        //识别id:分录id+创建时间点
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_CUSID   ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PIGSTY    ,showObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2  )) ;
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PLACE    ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2  ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_THREE    ,showObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2   ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAILREMARK   ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAILREMARK2 ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SAMGROUP      ,showObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMGROUP2   ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SAMPERIOD2    ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22  ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_INTERVALDAY  ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2   ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_UPORG     ,showObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_UPORG2   ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_STOPDETAIL    ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_STOPDETAIL2  ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_CHANGES    ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_CHANGES2 ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISPRESETS    ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2  ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_SUPID   ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_SUPID1  ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUHAS   ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUHAS1 ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUUPDATE  ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUUPDATE1  ));
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISLINE   ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISLINE1));
        //巡查位点 WENS_ENTRYDETAIL2_WENS_SITE2
        DynamicObject sitObj = showObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2);
        objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SITE  ,sitObj);
        //具体位置
        if(StringUtils.isBlank(ToolUtils.cnulls(showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2)))){
//            if(null!= sitObj){
//                objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SITEDETAILS   ,sitObj.getString(NAME));
//            }
        }else{
            objRes.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SITEDETAILS   ,showObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2 ));
        }
    }


    /**
     * @Author  coyle
     * @Description 对第三级孙单据体界面赋值
     * @Param [threeManyAndTwoVO]
     * @return void
     **/
    private void showDetailData(String parentId){
        IDataModel model = this.getModel();
        DynamicObjectCollection savedEntryCol = model.getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        List<DynamicObject> collect = savedEntryCol.stream().filter(object -> StringUtils.equals(object.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID), parentId))
                .sorted(Comparator.comparing(map -> map.get("wens_cus_seq") == null ? 0:map.getInt("wens_cus_seq"))).collect(Collectors.toList());
        //创建第三级数据
        DynamicObjectCollection showCol = model.getDataEntity(true).getDynamicObjectCollection(ENTRYTHREE);
        showCol.getDynamicObjectType();
        for (DynamicObject savedEntry : collect) {
            int seq = 0;
            DynamicObject showObj = new DynamicObject(showCol.getDynamicObjectType());
            ++seq;
            if(savedEntry.get("wens_cus_seq") == null ){
                savedEntry.set("wens_cus_seq",seq);
            }
            //写入界面对应字段----------------------
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PARENT_ROWID2,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_CUSID  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2      ,savedEntry.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PIGSTY    )) ;
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2       ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PLACE     ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2       ,savedEntry.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_THREE     ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2        ,savedEntry.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SITE      ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITEDETAILS2 ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SITEDETAILS  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAILREMARK2,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAILREMARK  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMGROUP2    ,savedEntry.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SAMGROUP  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22  ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SAMPERIOD2  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2 ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_INTERVALDAY  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_UPORG2       ,savedEntry.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_UPORG     ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_STOPDETAIL2  ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_STOPDETAIL  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_CHANGES2     ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_CHANGES   ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2   ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISPRESETS  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_SUPID1,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_DETAIL_SUPID  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUHAS1       ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUHAS     ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUUPDATE1    ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_SUUPDATE  ));
            showObj.set( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISLINE1      ,savedEntry.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISLINE  ));
            showCol.add(showObj);
        }
        model.setValue(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2,showCol);
        DynamicObjectCollection collection = getModel().getEntryEntity(ENTRYTHREE);
        getModel().updateEntryCache(collection);
        this.getView().updateView(ENTRYTHREE);
        if( (boolean)model.getValue(SchemePatrolPigFaConstant.WENS_UPDATE_STATUS) == true){
            colour("#FFFF00", false) ;
        }
    }



    /**
     * @Description 根据界面DynamicObject生成DetailEntryVO对象数据
     **/
    public SchemeDetailThreeManyVO setDetailEntryVO1(DynamicObject threeEntryObj){
        //获取老显示数据
        SchemeDetailThreeManyVO threeManyVO = new SchemeDetailThreeManyVO();
        //开始给数据对象封装
        if (null != threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2)) {
            threeManyVO.setPigsty(threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PIGSTY2).getString("id"));
        }
        if (null != threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2)) {
            threeManyVO.setThree(threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2).getString("id"));
        }
        if (null != threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMGROUP2)) {
            threeManyVO.setSamgroup(threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMGROUP2).getString("id"));
        }
        if (null != threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_UPORG2)) {
            threeManyVO.setUporg(threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_UPORG2).getString("id"));
        }
        if (null != threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2)) {
            threeManyVO.setSite(threeEntryObj.getDynamicObject(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2).getString("id"));
        }
        threeManyVO.setRowId(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_CUSID2));
        threeManyVO.setPlace(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_PLACE2));
        threeManyVO.setDetailremark(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAILREMARK2));
        threeManyVO.setSamperiod2(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22));
        threeManyVO.setIntervaldays(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2 ));
        threeManyVO.setDeactivate2(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_STOPDETAIL2));
        threeManyVO.setChanges(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_CHANGES2));
        threeManyVO.setIspresets(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2));
        threeManyVO.setParentRowId( threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_PARENT_ROWID)) ;
        threeManyVO.setSupId(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_DETAIL_SUPID1  ));
        threeManyVO.setSuHas(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUHAS1  ));
        threeManyVO.setSuUpdate(threeEntryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SUUPDATE1  ));
        threeManyVO.setIsline(threeEntryObj.getString( SchemePatrolPigFaConstant.WENS_ENTRYDETAIL_WENS_ISLINE ));
        return threeManyVO;
    }


    /**
     * 设置禁用状态
     */
    private   void  setFildEnable(){
        DynamicObjectCollection entryCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(SchemePatrolPigFaConstant.ENTRYENTITYID_WENS_ENTRYDETAIL2);
        int rowIndex = 0;
        ArrayList<String> enableVal = new ArrayList<>();
        ArrayList<String> unVal = new ArrayList<>();
        for(DynamicObject entryObj : entryCol){
            String periodType = ToolUtils.cnulls(entryObj.getString(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SAMPERIOD22));
            enableVal = new ArrayList<>();
            unVal = new ArrayList<>();
            if(StringUtils.equals(periodType,"1")){
                enableVal.add(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2);
            }else{
                unVal.add(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_INTERVALDAY2);
            }
            boolean ispresets =  entryObj.getBoolean(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_ISPRESETS2);
            if(ispresets){
                unVal.add(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2);
                unVal.add(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2);
            }else{
                enableVal.add(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_THREE2);
                enableVal.add(SchemePatrolPigFaConstant.WENS_ENTRYDETAIL2_WENS_SITE2);
            }
            this.getView().setEnable(true , rowIndex, enableVal.toArray(new String[enableVal.size()]));
            this.getView().setEnable(false, rowIndex, unVal.toArray(new String[unVal.size()]));
            rowIndex++;
        }
    }

    /**
     * 操作后事件
     */
    @Override
    public void afterDoOperation(AfterDoOperationEventArgs args) {
        super.afterDoOperation(args);
        FormOperate operate = (FormOperate) args.getSource();
        String operateKey = operate.getOperateKey();
        if((StringUtils.equals(operateKey,KEY_SAVE) || StringUtils.equals(operateKey,"submit")
                || StringUtils.equals(operateKey,"audit") || StringUtils.equals(operateKey,"unsubmit"))
                && (args.getOperationResult() != null
                && args.getOperationResult().isSuccess())){
            //刷新页面;
            this.getView().invokeOperation(P_REFRESH);
        }

    }



    /**
     * 设置必录
     * @param key
     */
    private void setMustInput(String key,boolean flag) {
        Control control = this.getControl(key);
        if (control instanceof BasedataEdit) {
            ((BasedataEdit) control).setMustInput(flag);
        } else if (control instanceof DateEdit) {
            ((DateEdit) control).setMustInput(flag);
        } else if (control instanceof TextEdit) {
            ((TextEdit) control).setMustInput(flag);
        }
    }



    @Override
    public void afterBindingData(AfterBindingDataEvent afterBindingDataEvent) {

    }


    /**
     * s设置序号
     */
    private  void  setSeqForEntry(){
        DynamicObjectCollection threeCol2 = this.getModel().getDataEntity(true).getDynamicObjectCollection(ENTRYTHREE);
        DynamicObjectCollection threeCol = this.getModel().getDataEntity(true).getDynamicObjectCollection(EnvSamSchemeConstant.ENTRYENTITYID_WENS_ENTRYDETAIL);
        int seq = 0;
        for(DynamicObject three2 :threeCol2){
            ++seq;
            String three2Id = ToolUtils.cnulls(three2.getString("wens_detail_cusid2"));
            List<Object> threeStre = threeCol.stream().filter(object -> StringUtils.equals(ToolUtils.cnulls(object.get("wens_detail_cusid")),
                    three2Id)).collect(Collectors.toList());
            if(threeStre.size() > 0 ){
                DynamicObject three = (DynamicObject) threeStre.get(0);
                three.set("wens_cus_seq" , three2.getInt("seq"));
            }
        }
    }



}
